<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                修改设备型号
            </el-header>
            <el-main>
                <el-form ref="editModelData" :rules="rules" :model="editModelData" label-width="115px"
                         label-position="left">
                    <el-form-item label="设备型号 :" prop="name">
                        <el-input v-model="editModelData.name" placeholder="请输入设备类型"></el-input>
                    </el-form-item>
                    <el-form-item label="设备类型 : " prop="tyidName">
                        <el-select v-model="editModelData.tyidName" placeholder="请选择设备类型" style="width: 100%;" @change="getValue()">
                            <el-option
                                    v-for="inst in typeOptions"
                                    :key="inst.uuid"
                                    :label="inst.name"
                                    :value="inst.name">
                                {{inst.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';
    export default {
        name: "editModel",
        props: {
            modelUuid: {
                type: String
            }
        },
        data() {
            return {
                formAlignLeft: 'left',
                editModelData: {
                    uuid: this.modelUuid,
                    name: '',
                    tyidName:'',
                    tyidUuid:''
                },
                typeOptions:[],
                rules: {
                    name: [
                        {required: true, message: '设备型号名称不得为空', trigger: 'blur'},
                    ],
                    tyidName: [
                        {required: true, message: '设备类型不得为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        //深度监听父组件的props传值
        watch: {
            modelUuid(val) {
                this.editModelData.uuid = val;
            }
        },
        mounted() {
            this.queryType();
            this.queryModel();
        },
        methods: {
            //查询设备类型
            queryType() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/tyid/search/findAll",
                }).then(response => {
                    this.typeOptions = response.data.content;
                }).catch(error => {
                    console.log(error);
                });
            },
            getValue(){
                this.typeOptions.forEach( list =>{
                    if(list.name === this.editModelData.tyidName){
                        this.editModelData.tyidUuid = list.uuid
                    }
                })
            },
            //通过uuid查询当前对象信息
            queryModel() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/modn/" + this.editModelData.uuid,
                    headers: {},
                    params: {}
                }).then(response => {
                    this.insertModel(response.data);
                }).catch(error => {
                    console.log(error);
                });
            },
            insertModel(editModelData) {
                this.editModelData.name = editModelData.name;
                this.editModelData.tyidName = editModelData.tyidName;
            },
            submit() {
                this.$refs['editModelData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "put",
                        url: "./smt/kdi/modn/" + this.editModelData.uuid,
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj(this.editModelData)
                    }).then(() => {
                        this.info = '修改成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = "修改失败";
                        this.infoShow = true;
                    });

                });
            },
            cleanEditModel() {
                this.editModelData = {
                    name: '',
                    tyidName:'',
                    tyidUuid:''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanEditModel();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>